/*
 * Created on Wed Jun 06 2018 by Ronnie Ren (zhaosong.ren) from Accenture
 *
 * Copyright (c) 2018 DingXing information & technology limited company.
 */

import { Injectable } from "@angular/core";
// import { UserService } from "../models/transform.service";
import { SqliteService } from "../../../services/sqliteDB/sqlite.service";
// import { BillService } from "./bill.service";

import {JsonMappingUtil} from "../../json-mapping-util";
import {InspectionManuals} from '../../transform/inspection/inspection-manuals';
import {InspectionManualsDto} from '../../dto/inspection/inspection-manuals.dto';
import { Observable } from "rxjs";
import { CurdService } from "../curd.service";
import { SyncUtilService } from "../../../services/offline/sync.util.service";



@Injectable()
export class InspectionManualsService {

    constructor(
        private sqliteService: SqliteService,
        private curdService: CurdService,
        private syncUtilService: SyncUtilService
    ) {

    }

    getManuals(plannedId: string) {
        return  this.curdService.observableFetchObjects(InspectionManualsDto , {taskId: plannedId})
        .map((objs: Array<{}>) => {
            if (objs && objs.length > 0) {
                return objs.map((obj) => {
                    let dto = new InspectionManualsDto();
                    return JsonMappingUtil.MappingAllProp(obj, dto);
                });
            }
            return [];
        })
        .map((dtos) => dtos.map((dto) => {
            if (dto) {
                let model = new InspectionManuals({});
                return JsonMappingUtil.MappingAllProp(dto, model);
            }
            return null;

        }));
    }

    getManualAttachment(id: string) {
        return  this.curdService.observableFetchObjects(InspectionManualsDto , {attachmentId: id})
        .map((objs: Array<{}>) => {
            if (objs && objs.length > 0) {
                return objs.map((obj) => {
                    let dto = new InspectionManualsDto();
                    return JsonMappingUtil.MappingAllProp(obj, dto);
                });
            }
            return [];
        })
        .map((dtos) => dtos.map((dto) => {
            if (dto) {
                let model = new InspectionManuals({});
                return JsonMappingUtil.MappingAllProp(dto, model);
            }
            return null;

        }));
    }

    getManualAttachmentByAttachmentAndId(attachmentId: string, id: string) {
      return  this.curdService.observableFetchObjects(InspectionManualsDto , {attachmentId: attachmentId, id: id})
        .map((objs: Array<{}>) => {
          if (objs && objs.length > 0) {
            return objs.map((obj) => {
              let dto = new InspectionManualsDto();
              return JsonMappingUtil.MappingAllProp(obj, dto);
            });
          }
          return [];
        })
        .map((dtos) => dtos.map((dto) => {
          if (dto) {
            let model = new InspectionManuals({});
            return JsonMappingUtil.MappingAllProp(dto, model);
          }
          return null;

        }));
    }

    updateManuals(data) {
        return this.curdService.updateRow(
            this.syncUtilService.toObjectDto(data, InspectionManualsDto),
            {taskId:  data.taskId});
    }

    updateManualsByTaskIdAndId(data) {
      let id = data.id;
      delete data.id;
      return this.curdService.updateRow(
        this.syncUtilService.toObjectDto(data, InspectionManualsDto),
        {taskId:  data.taskId, id: id});
    }
}
